﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;

namespace ExaminationSys
{
    
    public partial class Testhistory : BasePage
    {
        private static readonly LoginLogService Proxy = ObjectFactory<LoginLogService>.Create();
        private static readonly TeacherInfoService ProxyTeacherInfo = ObjectFactory<TeacherInfoService>.Create();
        private static readonly SubSectionInfoService ProxySubSection = ObjectFactory<SubSectionInfoService>.Create();
        private static readonly StudentTestHistoryService ProxyStudentTestHistory = ObjectFactory<StudentTestHistoryService>.Create();
        private static readonly SubSectionDetailInfoService ProxySubSectionDetailInfo = ObjectFactory<SubSectionDetailInfoService>.Create();
        private static readonly PaperInfoService ProxyPaperinfo = ObjectFactory<PaperInfoService>.Create();

        public List<SubSectionInfo> SubSectionInfoCollection = new List<SubSectionInfo>();


        public List<SubSectionDetailInfo> GetListBySubSectionId(Guid subSectionid)
        {
            return ProxySubSectionDetailInfo.GetListBySubSectionId(subSectionid);
        }

        public SubSectionDetailInfo CurrentSelectItem = new SubSectionDetailInfo();
        public List<StudentTestHistorySearchEntity> StudentTestHistoryCollection = new List<StudentTestHistorySearchEntity>(); 

        //级别名称
        public string LevelName { get; set; }
        public int KnowlegdeLevel = 0;
        public Guid ID { get; set; }
        public string TeacherName { get; set; }
        public StringBuilder htmlStr = new StringBuilder();
        public StringBuilder htmlStrHistory = new StringBuilder();
        public StringBuilder htmlStrPager = new StringBuilder();
        public string LoginName { get; set; }
        public int TotalPage { get; set; }
        public int TotalRecordCount { get; set; }
        public int CurrentPageIndex { get; set; }
        private int pageSize = 50;
        public int EndLoopIndex { get; set; }
        public int Loop = 15;
        public Guid sid = Guid.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            KnowlegdeLevel = base.GetCurrentStudentLevel();
            var idTmp = Request.QueryString["sid"];
            Guid.TryParse(idTmp, out sid);
        
            if (sid == Guid.Empty)
            {
                Response.Redirect("error.aspx");
                Response.End();
            }
            if (!IsPostBack)
            {
                base.ResetSession();
                var loginInfo = Session["StudentInfo"] as Student;
                if (loginInfo != null)
                {
                    LoginName = loginInfo.UserName;
                    if (KnowlegdeLevel == 0) KnowlegdeLevel = (int) EnumLevel.初级;
                    LevelName = Enum.GetName(typeof (EnumLevel), KnowlegdeLevel);
                    SubSectionInfoCollection = ProxySubSection.GetListByKnowlegdeLevel(KnowlegdeLevel,idTmp);


                    //(string sqlwhere, string orderbyFiled, int pagesize, int pageindex, out int totalPage, out int totalRecord)
                    //get list by page
                    var pageIndexTmp = Request["pageIndex"];
                    int pageIndex = 0;
                    int.TryParse(pageIndexTmp, out pageIndex);

                    string sqlwhere =
                        @"status=1 and StudentID='{0}' and SectionID='{1}' ";

                    if (pageIndex <= 0) pageIndex = 1;
                    int totalPage, recordCount = 0;
                    StudentTestHistoryCollection =
                        ProxyStudentTestHistory.GetHistoryListByPage(
                            string.Format(sqlwhere, loginInfo.ID,sid),
                            "TestStartTime DESC",
                            pageSize, pageIndex,
                            out totalPage, out recordCount);
                    CurrentPageIndex = pageIndex;

                    int endloop = 0;
                    if (CurrentPageIndex % Loop == 0) EndLoopIndex = CurrentPageIndex / Loop + 1;
                    else EndLoopIndex = CurrentPageIndex/Loop;
                    EndLoopIndex = EndLoopIndex*Loop;
                    if (StudentTestHistoryCollection != null && StudentTestHistoryCollection.Count > 0)
                    {
                        PaperInfo p;
                        foreach (var item in StudentTestHistoryCollection)
                        {
                            p = ProxyPaperinfo.Get(item.PaperInfoID);

                            if (p != null) item.PaperTitle = p.Title;
                        }
                    }

                    TotalPage = totalPage;
                    TotalRecordCount = recordCount;

                }
                else
                {
                    Response.Redirect("Index.aspx");
                }
            }
        }
    }
}